package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import data.BankDatabaseUserSQL;

/**
 * @author Matthias
 *
 */
public class UserMenuViewAcc extends JPanel {

	private final JButton btnBack;
	// jtable for displaying data from the sql database
	private final JTable table; 
	private final JLabel lblHeader;
	private final JLabel logoATM;

	/** Declaring the BankDatabaseUserSQL object by the type obtainData **/
	private final BankDatabaseUserSQL obtainData;
	
	/** Declaring the Screen object by the type screen **/
	private final Screen screen;

	/**
	 * @param screen
	 */
	public UserMenuViewAcc(final Screen screen) {

		this.screen = screen;
		this.obtainData = screen.getClassATM().getClassBdbUserSQL();

		setLayout(null);
		setPreferredSize(new Dimension(600, 480));

		table = new JTable();
		table.setFont(new Font("SansSerif", Font.PLAIN, 12));
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 78, 580, 200);
		add(scrollPane);

		btnBack = new JButton("Back");
		btnBack.setBounds(100, 350, 130, 50);
		btnBack.setFont(new Font("SansSerif", Font.PLAIN, 16));
		add(btnBack);

		lblHeader = new JLabel("View all your accounts");
		lblHeader.setFont(new Font("SansSerif", Font.BOLD, 26));
		lblHeader.setBounds(160, 25, 281, 34);
		add(lblHeader);

		logoATM = new JLabel("");
		logoATM.setIcon(new ImageIcon(UserMenuDelete.class.getResource("/pic/LOGO Mini.png")));
		logoATM.setBounds(15, 15, 50, 50);
		add(logoATM);
	}

	/** 
	 * @return a query of all the account data for the current user,
	 * making it a maximum of 3, since each user only can have 3 accounts
	 * However, the user can't do any SQL commands, but instead just see the data
	 */
	public String getQuery() {
		String sql = "SELECT * FROM DB_ACCOUNT WHERE User_ID="+screen.getClassATM().getCurrentUserID();
		return sql;
	}

	/** Adds action listeners, in order to define an action 
	 * when a component is called
	 * @param e
	 */
	public void addActionListener(final ActionListener e){
		btnBack.addActionListener(e);
	}

	/** 
	 * @return back button 
	 */
	public JButton getBtnBack(){
		return btnBack;
	}

	/** @see ui.AdmView#displayQueryResults(String)
	 * @param q
	 */
	public void displayQueryResults(final String q) {
		try {
			table.setModel(obtainData.viewDB(q));  
		}
		catch (SQLException ex) {
			ex.getMessage();
		}
	}
	
} // end this class